package com.hzsparrow.ai.controller;

import com.hzsparrow.ai.entity.KnowledgeBase;
import com.hzsparrow.ai.entity.User;
import com.hzsparrow.ai.service.FileService;
import com.hzsparrow.ai.service.KnowledgeBaseService;
import com.hzsparrow.ai.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * 文件上传控制器
 */
@RestController
@RequestMapping("/api/files")
public class FileUploadController {

    @Autowired
    private FileService fileService;
    
    @Autowired
    private UserService userService;
    
    @Autowired
    private KnowledgeBaseService knowledgeBaseService;
    
    /**
     * 文件上传接口
     */
    @PostMapping("/upload")
    public ResponseEntity<?> uploadFile(@RequestParam("file") MultipartFile file) {
        try {
            // 获取当前用户
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            User user = userService.getUserByUsername(authentication.getName());
            
            // 上传文件
            Map<String, Object> fileInfo = fileService.uploadFile(file);
            
            return ResponseEntity.ok(fileInfo);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body("文件上传失败: " + e.getMessage());
        }
    }
    
    /**
     * 上传文件并关联到知识库
     */
    @PostMapping("/upload-to-knowledge-base")
    public ResponseEntity<?> uploadToKnowledgeBase(
            @RequestParam("file") MultipartFile file,
            @RequestParam("knowledgeBaseId") Long knowledgeBaseId) {
        try {
            // 获取当前用户
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            User user = userService.getUserByUsername(authentication.getName());
            
            // 验证知识库
            KnowledgeBase knowledgeBase = knowledgeBaseService.getKnowledgeBase(knowledgeBaseId);
            if (knowledgeBase == null) {
                return ResponseEntity.badRequest().body("知识库不存在");
            }
            
            // 验证权限
            if (!knowledgeBase.getCreateUserId().equals(user.getId()) && !user.getRole().equals("ROLE_ADMIN")) {
                return ResponseEntity.status(403).body("无权向此知识库添加文档");
            }
            
            // 上传文件
            Map<String, Object> fileInfo = fileService.uploadFile(file);
            
            // 返回文件信息，但不自动创建文档
            return ResponseEntity.ok(fileInfo);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body("文件上传失败: " + e.getMessage());
        }
    }
} 